var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal:false,
		isSave:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityLibId: options.commodityLibId,
      identifier: options.identifier
    })
    this.getLibDetail()
  },

  // 获取商品库详细信息
  getLibDetail(){
    var commodityLibId = this.data.commodityLibId

    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this
    wx.request({
      url: baseUrl + '/commodity_lib/' + commodityLibId,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var libDetail = res.data.data
          var params
          for (var i = 0; i < libDetail.commodityLibDetail.length;i++){
            params = JSON.parse(libDetail.commodityLibDetail[i].params)
            libDetail.commodityLibDetail[i].params = params;
          }
          console.log("libDetail = ", libDetail)
          that.setData({
            libDetail: libDetail
          })
          
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 调节商品库参数
  select(res){
    var libDetail = this.data.libDetail
    var index = res.currentTarget.dataset.index
    var idx = res.currentTarget.dataset.idx
    var values = libDetail.commodityLibDetail[index].params.goodsDef[idx].values
    var title = libDetail.commodityLibDetail[index].params.goodsDef[idx].name+"调节"
    var pvalue = libDetail.commodityLibDetail[index].params.goodsDef[idx].value*1-1
    var list = []
    var idList = []
    for(var key in values){
      idList.push(key)
      list.push(values[key])
    }
    this.setData({
      idList:idList,
      list:list,
      showModal:true,
      title:title,
      pvalue:pvalue,
      commodityLibIndex:index,
      goodsDefIndex:idx
    })
  },
  // 确定修改枚举参数
  confirm(){
    var libDetail = this.data.libDetail
    var idList = this.data.idList
    var pvalue = this.data.pvalue
    var idx = this.data.goodsDefIndex
    var index = this.data.commodityLibIndex
    libDetail.commodityLibDetail[index].params.goodsDef[idx].value = idList[pvalue]+""
    this.setData({
      libDetail: libDetail
    })
    console.log("libDetail = ", libDetail)
    this.cancel()
  },
  // 取消修改枚举参数
  cancel(){
    this.setData({
      showModal:false
    })
  },
  // 选择枚举参数
  editItem: function (res) {
    var pvalue = res.currentTarget.dataset.value
    this.setData({
      pvalue: pvalue
    })
  },

  // 参数调节输入框聚焦
  focusInput: function (res) {
    var commodityLibIndex = res.currentTarget.dataset.index
    var goodsDefIndex = res.currentTarget.dataset.idx
    console.log("commodityLibIndex = ", commodityLibIndex)
    console.log("goodsDefIndex = ", goodsDefIndex)
    this.setData({
      commodityLibIndex: commodityLibIndex,
      goodsDefIndex: goodsDefIndex
    })
  },

  // 参数输入框
  paramInput: function (res) {
    var libDetail = this.data.libDetail
    var index = this.data.commodityLibIndex
    var idx = this.data.goodsDefIndex
    var pvalue = res.detail.value * 1
    libDetail.commodityLibDetail[index].params.goodsDef[idx].value = pvalue
    this.setData({
      libDetail: libDetail
    }) 
    //console.log("libDetail = ", libDetail)
  },

  // 保存全部参数
  save(){
		this.data.isSave = false;
    var libDetail = this.data.libDetail
    var params;
		//console.log(libDetail.commodityLibDetail)
    for (var i = 0; i < libDetail.commodityLibDetail.length;i++){
      params = JSON.stringify(libDetail.commodityLibDetail[i].params)
      libDetail.commodityLibDetail[i].params = params
    }
    var commodityLibDetails = libDetail.commodityLibDetail
    var commodityLibName = libDetail.commodityName
    var deleteLibDetail = []
    var productId = libDetail.productId
    var commodityLibId = libDetail.commodityLibId
    var remark = libDetail.remark
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this
    wx.request({
      url: baseUrl + 'commodity_lib/' + commodityLibId,
      method:"put",
      data:{
        commodityLibDetails: commodityLibDetails,
        commodityLibName: commodityLibName,
        deleteLibDetail: deleteLibDetail,
        productId: productId,
        remark: remark
      },
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            duration: 3500
          })						
					that.getLibDetail();
					that.data.isSave = true;

        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})